<template>
  <div>
    <h1>当前求和为：{{ sum }}</h1>
    <h3>学校: {{ school }},学习: {{ subject}}</h3>
    <!-- (1) 下拉选择框获得的数据默认是 字符串，可以通过在双向绑定v-model加上number
             强制转化
    -->
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementOdd">当前和为奇数再加</button>
    <button @click="incrementWait">等一等再加</button>
  </div>
</template>

<script>
export default {
  name: "myCount",
  data() {
    return {
      n: 1,
    };
  },
  methods: {
    increment() {
      this.$store.commit("Add", this.n);
    },
    decrement() {
      this.$store.commit("Sub", this.n);
    },
    incrementOdd() {
      this.$store.dispatch("addOdd", this.n);
    },
    incrementWait() {
      this.$store.dispatch("addWait", this.n);
    },
  },
  // (2) 通过计算属性来让模板表达式尽可能简单，相当于在本地组件里再重新添加简写形式
  computed: {
    sum(){
      return this.$store.state.sum
    },
    school(){
      return this.$store.state.school
    },
    subject(){
      return this.$store.state.subject
    },
    bigSum(){
      return this.$store.getters.bigSum
    },
  },
};
</script>
